package com.example.fashion.android.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.fashion.android.R;
import com.example.fashion.android.base.BaseActivity.BaseActivity;
import com.example.fashion.android.base.BaseAdapter.BaseRecyclerAdapter;
import com.example.fashion.android.base.BaseAdapter.RecyclerViewHolder;
import com.example.fashion.android.home.adapter.HomeAdapter;
import com.example.fashion.android.globar.Constant;
import com.example.fashion.android.util.RecyclerSpace;

import java.util.ArrayList;
import java.util.List;

/**
 * Android目录界面
 */
public class HomeActivity extends BaseActivity {
    RecyclerView mRecyclerView;
    HomeAdapter mHomeAdapter;
    List mCatalogList ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    }

    /**
     * 初始化View
     */
    @Override
    public void initView() {
        //隐藏标题栏
        hideSystemTitleBar();

        mCatalogList = new ArrayList();
        //加载目录数据
        String[] catalogS = getResources().getStringArray(Constant.CATALOG_RESOURCE_ID);
        for (int i=0;i<catalogS.length;i++){
            mCatalogList.add(catalogS[i]);
        }
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

    }

    /**
     * 初始化数据
     */
    @Override
    public void initData() {
        //配置RecyclerView
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
        mRecyclerView.addItemDecoration(new RecyclerSpace(10));
        mHomeAdapter = new HomeAdapter(mRecyclerView, mCatalogList, R.layout.item_home);
        mHomeAdapter.setonItemViewClickListener(new BaseRecyclerAdapter.OnItemViewClickListener() {
            @Override
            public void onItemViewClick(RecyclerViewHolder view, int position) {
                switch (position){
                    case 0:     //四大组件
                        startActivity(new Intent(HomeActivity.this,FourComponents.class));
                        break;
                    case 1:     //网络通讯
                        startActivity(new Intent(HomeActivity.this,NetWorkCommunicationActivity.class));
                        break;
                    case 2:     //数据持久化
                        break;
                    case 3:     //UI控件与Demo
                        startActivity(new Intent(HomeActivity.this,DemoActivity.class));
                        break;
                    case 4:     //多媒体
                        startActivity(new Intent(HomeActivity.this,MediaActivity.class));
                        break;
                    case 5:     //图片处理
                        break;
                    case 6:     //第三方扩展
                        break;
                    case 7:     //性能优化
                        break;
                    case 8:     //数据安全
                        break;
                    default:
                        break;
                }
            }
        });
        mRecyclerView.setAdapter(mHomeAdapter);
    }

    /**
     * 如果点击返回,则退出程序
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mAppManager.exit();
    }

    @Override
    protected void clear() {
        if(mCatalogList!=null){
            mCatalogList.clear();
            mCatalogList = null;
        }
    }

}
